package com.bwie.student.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.domain.Clazz;
import com.bwie.common.domain.Student;
import com.bwie.common.domain.request.StudentRequest;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.common.utils.FastUtil;
import com.bwie.student.mapper.StudentMapper;
import com.bwie.student.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author：ShenJiaFeng
 * @Package：com.bwie.student.service.impl
 * @Project：zg5_19
 * @name：StudentServiceImpl
 * @Date：2024/2/17 8:59
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
implements StudentService {

    private final StudentMapper studentMapper;



    @Autowired
    private FastUtil fastUtil;


    public StudentServiceImpl(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }
    @Override
    public Result<PageResult<Student>> studentList(StudentRequest studentRequest) {
        PageHelper.startPage(studentRequest.getPageNum(),studentRequest.getPageSize());
        List<Student> students = studentMapper.studentList(studentRequest);
        PageInfo<Student> info = new PageInfo<>(students);
        return PageResult.toResult(info.getTotal(),students);
    }

    @Override
    public Result<Student> addStudentList(Student student) {
        Integer i = studentMapper.addStudentList(student);
        if (i > 0){
            return Result.success(student,"新增成功");
        }

        return Result.error("新增失败");
    }



    @Override
    public Result delStudent(Integer studentId) {
        Integer i = studentMapper.delStudent(studentId);
        if (i > 0){
            return Result.success(studentId,"删除成功了");
        }
        return Result.error("删除失败");
    }

    @Override
    public Result fastUpload(MultipartFile multipartFile) {
//        FastUtil fastUtil = new FastUtil();
        String upload="";
        try {
            upload=fastUtil.upload(multipartFile);
            if (upload.length()<=0){
                return Result.error("上传失败");
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return Result.success("http://124.222.71.202:8888/"+upload,"上传成功");
    }

    @Override
    public Result<List<Student>> studentLists() {
        List<Student> students = studentMapper.studentLists();
        return Result.success(students);
    }

    @Override
    public Result<Student> updStudent(Student student) {
        Integer i = studentMapper.updStudent(student);
        if (i > 0){
            return Result.success(student,"编辑成功");
        }
        return Result.error("编辑失败");
    }

    @Override
    public Result<Student> findByStudent(Integer studentId) {
        Student byStudent = studentMapper.findByStudent(studentId);
        return Result.success(byStudent,"学生信息");
    }

    @Override
    public Result clazzList() {
        List<Clazz> clazzes = studentMapper.clazzList();
        return Result.success(clazzes);
    }

    @Override
    public Result deleteStudent(Integer studentId) {
        Integer i = studentMapper.deleteStudent(studentId);

        if (i > 0){
            return Result.success(studentId,"删除成功");
        }

        return Result.error("删除失败");
    }

}
